use crate::api::*;
use crate::boot::db;
use crate::boot::err::MyError;
use crate::lib::util_string;
use crate::model::SysOrgan;
use crate::model::SysRole;

use crate::api::router::AppState;
use actix_web::{cookie::Cookie, web, HttpRequest, HttpResponse, Result};
use async_session::{MemoryStore, Session, SessionStore as _};
use rbs::Value as rbv;
use serde_json::Value;
use std::collections::HashSet;

// 测试1
pub async fn test1(req: HttpRequest, data: web::Data<AppState>) -> Result<HttpResponse, MyError> {
    log::info!("11111111111111111 test1 1111111111111111");

    let c = req.cookies().map_err(|e| MyError::Other(e.into()));
    log::info!("读取 cookies {:?}", c);

    let mut store = data.store.clone();

    // Create a new session.
    let mut session = Session::new();
    let v = session
        .insert("user_id", 11221)
        .map_err(|e| MyError::Other(e.into()))?;

    let cookie_id = store.store_session(session).await.unwrap().unwrap();
    log::info!("生成的cookieid ： {}", cookie_id);

    let cookie = Cookie::build("LB-session-id", cookie_id)
        .secure(false)
        .http_only(false)
        .finish();

    Ok(HttpResponse::Ok().cookie(cookie).body(""))
}

// 测试1
pub async fn test2(req: HttpRequest, data: web::Data<AppState>) -> Result<HttpResponse, MyError> {
    log::info!("2222222222222222 test2 222222222222222222222");

    // 获取 session_id
    if let Some(session_cookie) = req.cookie("LB-session-id") {
        let cookie_value = session_cookie.value();

        log::info!("获取到的 LB-session-id ： {} ", cookie_value);

        let mut store = data.store.clone();

        let ss = store
            .load_session(cookie_value.into())
            .await
            .map_err(|e| MyError::Other(e.into()))?;
        if let Some(session) = ss {
            let v = session.get::<i32>("user_id").unwrap_or(0);

            log::info!("从session中读取数据:{}", v);
        } else {
            log::info!("获取 session 失败！");
        }
    } else {
        log::info!("从 cookie 中获取 LB-session-id 失败！")
    }

    resp_err(-1, "读取 session")
}


// 测试1
pub async fn test3(req: HttpRequest, data: web::Data<AppState>) -> Result<HttpResponse, MyError> {
    log::info!("11111111111111111 test1 1111111111111111");

    let c = req.cookies().map_err(|e| MyError::Other(e.into()));
    log::info!("读取 cookies {:?}", c);

    let store = db::SDB.clone();

    // Create a new session.
    let mut session = Session::new();
    let v = session
        .insert("user_id", 1112212221)
        .map_err(|e| MyError::Other(e.into()))?;

    let cookie_id = store.store_session(session).await.unwrap().unwrap();
    log::info!("生成的cookieid ： {}", cookie_id);

    let cookie = Cookie::build("LB-session-id", cookie_id)
        .secure(false)
        .http_only(false)
        .finish();

    Ok(HttpResponse::Ok().cookie(cookie).body(""))
}

// 测试1
pub async fn test4(req: HttpRequest, data: web::Data<AppState>) -> Result<HttpResponse, MyError> {
    log::info!("2222222222222222 test2 222222222222222222222");

    // 获取 session_id
    if let Some(session_cookie) = req.cookie("LB-session-id") {
        let cookie_value = session_cookie.value();

        log::info!("获取到的 LB-session-id ： {} ", cookie_value);

        let store = db::SDB.clone();

        let ss = store
            .load_session(cookie_value.into())
            .await
            .map_err(|e| MyError::Other(e.into()))?;
        if let Some(session) = ss {
            let v = session.get::<i32>("user_id").unwrap_or(0);

            log::info!("从session中读取数据:{}", v);
        } else {
            log::info!("获取 session 失败！");
        }
    } else {
        log::info!("从 cookie 中获取 LB-session-id 失败！")
    }

    resp_err(-1, "读取 session")
}